#include "audiodecodeimpl.h"
#include "audiostreamimpl.h"
AudioStreamImpl::AudioStreamImpl(AVStream *stream, AudioAbstract *event):
    StreamAbstract(stream),
    bInit(false)
{
    m_oAudioDecodeImpl = new AudioDecodeImpl(event);
}

AudioStreamImpl::~AudioStreamImpl()
{
    delete m_oAudioDecodeImpl;
}

int AudioStreamImpl::GetDecodeAudio(char *data, int len)
{
    if(m_oAudioDecodeImpl)
    {
        return m_oAudioDecodeImpl->read_data(data,len);
    }
    return 0;
}

bool AudioStreamImpl::is_init()
{
    return bInit;
}

bool AudioStreamImpl::Init()
{
    bInit = true;
    if(m_oAudioDecodeImpl && m_oAudioDecodeImpl->Init(GetStream()))
    {
        return true;
    }
    return false;
}

bool AudioStreamImpl::UnInit()
{
    if(m_oAudioDecodeImpl && m_oAudioDecodeImpl->UnInit())
    {
        return true;
    }
    return false;
}

bool AudioStreamImpl::receive_handler(const char *data, int size)
{
    if(m_oAudioDecodeImpl)
    {
        while(true)
        {
            if (m_oAudioDecodeImpl->get_queue_size() > (100))
            {
               av_usleep(10*1000);
            }else
            {
                break;
            }
        }
        m_oAudioDecodeImpl->receive_handler(data,size);
    }
    return true;
}

double AudioStreamImpl::time_stamp()
{
    if(m_oAudioDecodeImpl)
       return  m_oAudioDecodeImpl->get_audio_clock();
    return 0;
}

